package com.erp.database.task;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.erp.database.pos.domain.MyEcrGoodsDomain;
import com.erp.database.pos.domain.MyEcrHSaleDetailDomain;
import com.erp.database.pos.domain.POSGoodsDomain;
import com.erp.database.pos.domain.POSSaleDetailDomain;
import com.erp.database.pos.service.MiddleTableService;
import com.erp.database.pos.service.MyEcrService;
import com.erp.database.pos.service.SendEmailService;


/** 
 * @author Buddy
 * @version 2017年10月12日 下午3:29:06 
 */
@Component
@EnableScheduling
public class POSSchedule {

	private static Logger logger = LoggerFactory.getLogger(POSSchedule.class);
	
	@Autowired
	private MyEcrService myEcrService;
	
	@Autowired
	private MiddleTableService middleTableService;
	
	/**
	 * 同步POS机的商品信息
	 * @throws Exception 
	 */
	@Scheduled(cron="00 00 21 * * ? ")//每天21:00同步当天的商品信息
//	@Scheduled(cron="00 * * * * ? ")//每分钟同步一次
	public void synGoods() throws Exception {
        logger.info("------------------------------->schedule 同步POS商品信息 begin...... ");
        try {
        	List<MyEcrGoodsDomain> myEcrGoodsList = myEcrService.synGoods();
        	int myEcrGoodsSize = myEcrGoodsList.size(); 
        	logger.info("--------------------->获取到的数据"+myEcrGoodsList.size()+"条");
        	
        	List<POSGoodsDomain>  posGoodsList = new ArrayList<POSGoodsDomain>();
        	
        	int pageNum = myEcrGoodsSize/100+1;
        	int colNum = 0;
        	for(int page=1;page<pageNum+1;page++){
        		colNum = page*100<myEcrGoodsSize?page*100:myEcrGoodsSize;
        		for(int col=(page-1)*100;col<colNum;col++){
            		POSGoodsDomain posGoodsDomain = new POSGoodsDomain();
            		BeanUtils.copyProperties(myEcrGoodsList.get(col), posGoodsDomain);
            		posGoodsList.add(posGoodsDomain);
        		}
            	middleTableService.insertOrUpdatePosGoods(posGoodsList);
            	logger.info("--------------------->第"+page+"页，同步"+posGoodsList.size()+"条数据，一共"+pageNum+"页");
            	posGoodsList.clear();
        	}
        	
        } catch (Exception e) {
            e.printStackTrace();
            SendEmailService.sendEmail(e.getMessage());
        }
        logger.info("------------------------------->schedule 同步POS商品信息 end ! ");
    }
	
	
	@Scheduled(cron="00 00 22 * * ? ")//每天22:00同步当天的订单信息
//	@Scheduled(cron="00 * * * * ? ")//每分钟同步一次
	public void synSaleDetail() throws Exception {
        logger.info("------------------------------->schedule 同步POS销售订单信息 begin...... ");
        try {
        	List<MyEcrHSaleDetailDomain> MyEcrHSaleDetailList = myEcrService.synSaleDetail();
        	int myEcrHSaleDetailSize = MyEcrHSaleDetailList.size(); 
        	logger.info("--------------------->获取到的数据"+MyEcrHSaleDetailList.size()+"条");
        	
        	List<POSSaleDetailDomain>  posSaleDetailList = new ArrayList<POSSaleDetailDomain>();
        	
        	int pageNum = myEcrHSaleDetailSize/100+1;
        	int colNum = 0;
        	for(int page=1;page<pageNum+1;page++){
        		colNum = page*100<myEcrHSaleDetailSize?page*100:myEcrHSaleDetailSize;
        		for(int col=(page-1)*100;col<colNum;col++){
            		POSSaleDetailDomain posSaleDetailDomain  = new POSSaleDetailDomain();
            		BeanUtils.copyProperties(MyEcrHSaleDetailList.get(col), posSaleDetailDomain);
            		posSaleDetailList.add(posSaleDetailDomain);
        		}
            	middleTableService.insertPosSaleDetail(posSaleDetailList);
            	logger.info("--------------------->第"+page+"页，同步"+posSaleDetailList.size()+"条数据，共"+pageNum+"页");
            	posSaleDetailList.clear();
        	}
        	
        } catch (Exception e) {
            e.printStackTrace();
            SendEmailService.sendEmail(e.getMessage());
        }
        logger.info("------------------------------->schedule 同步POS销售订单信息 end ! ");
    }
	
}
